﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class MenuListApp : IMenuList
    {
        private string dbname = "Systems";

        public void Add(MenuListEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_MenuList(");
            builder.Append("MenuSId,MenuName,MenuText,MenuDept,MenuUrl,MenuPicAddress,ISFMenu,MenuViisib,MenuTaxisNum,IsSuperMenu");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.MenuSId + ",");
            builder.Append("'" + model.MenuName + "',");
            builder.Append("'" + model.MenuText + "',");
            builder.Append("" + model.MenuDept + ",");
            builder.Append("'" + model.MenuUrl + "',");
            builder.Append("'" + model.MenuPicAddress + "',");
            builder.Append("" + model.ISFMenu + ",");
            builder.Append("" + model.MenuViisib + ",");
            builder.Append("" + model.MenuTaxisNum + ",");
            builder.Append("" + model.IsSuperMenu + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int MenuId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_MenuList ");
            builder.Append(" where MenuId=" + MenuId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_MenuList ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by MenuId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "MenuId", "T_MenuList", swhere);
        }

        public MenuListEnt GetModel(int MenuId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MenuId,MenuSId,MenuName,MenuText,MenuDept,MenuUrl,MenuPicAddress,ISFMenu,MenuViisib,MenuTaxisNum,IsSuperMenu from T_MenuList ");
            builder.Append(" where MenuId=" + MenuId);
            MenuListEnt ent = new MenuListEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.MenuId = MenuId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["MenuSId"].ToString() != "")
                {
                    ent.MenuSId = int.Parse(table.Rows[0]["MenuSId"].ToString());
                }
                ent.MenuName = table.Rows[0]["MenuName"].ToString();
                ent.MenuText = table.Rows[0]["MenuText"].ToString();
                if (table.Rows[0]["MenuDept"].ToString() != "")
                {
                    ent.MenuDept = int.Parse(table.Rows[0]["MenuDept"].ToString());
                }
                ent.MenuUrl = table.Rows[0]["MenuUrl"].ToString();
                ent.MenuPicAddress = table.Rows[0]["MenuPicAddress"].ToString();
                if (table.Rows[0]["ISFMenu"].ToString() != "")
                {
                    ent.ISFMenu = int.Parse(table.Rows[0]["ISFMenu"].ToString());
                }
                if (table.Rows[0]["MenuViisib"].ToString() != "")
                {
                    ent.MenuViisib = int.Parse(table.Rows[0]["MenuViisib"].ToString());
                }
                if (table.Rows[0]["MenuTaxisNum"].ToString() != "")
                {
                    ent.MenuTaxisNum = int.Parse(table.Rows[0]["MenuTaxisNum"].ToString());
                }
                if (table.Rows[0]["IsSuperMenu"].ToString() != "")
                {
                    ent.IsSuperMenu = int.Parse(table.Rows[0]["IsSuperMenu"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_MenuList ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "MenuId");
        }

        public void Update(MenuListEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_MenuList set ");
            builder.Append("MenuSId=" + model.MenuSId + ",");
            builder.Append("MenuName='" + model.MenuName + "',");
            builder.Append("MenuText='" + model.MenuText + "',");
            builder.Append("MenuDept=" + model.MenuDept + ",");
            builder.Append("MenuUrl='" + model.MenuUrl + "',");
            builder.Append("MenuPicAddress='" + model.MenuPicAddress + "',");
            builder.Append("ISFMenu=" + model.ISFMenu + ",");
            builder.Append("MenuViisib=" + model.MenuViisib + ",");
            builder.Append("MenuTaxisNum=" + model.MenuTaxisNum + ",");
            builder.Append("IsSuperMenu=" + model.IsSuperMenu + "");
            builder.Append(" where MenuId=" + model.MenuId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

